function secondLargest(arr) {
  let index2nd = arr.length - 2;
  let times = 2;
  for (let i=0; i < times; i++) {
    for (let j=0 ; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j+1]) {
        [arr[j], arr[j+1]] = [arr[j+1], arr[j]];
      }
    }
    if (arr[index2nd + 1] === arr[index2nd]) {
      times++;
      index2nd--;
    }
  }
  return arr[index2nd];
}

console.log(secondLargest([4, 3, 2, 1])); // 3
console.log(secondLargest([2, 3, 4, 6, 6])); // 4
console.log(secondLargest([1, 0, -1, 0, 1, 0, -1, 1, 0])); // 0